package com.lideru.backstage.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lideru.backstage.entity.Chapter;
import com.lideru.backstage.entity.User;
import com.lideru.backstage.handler.NoAuth;
import com.lideru.backstage.mapper.ChapterMapper;
import com.lideru.backstage.utils.JWTUtils;
import com.lideru.backstage.utils.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * @author lideru
 */
@RestController
@RequestMapping("/chapter")
public class ChapterController {
    @Resource
    private ChapterMapper chapterMapper;


    @PostMapping
    public Result<?> save(@RequestBody Chapter chapter,HttpServletRequest request) {
        Long userId ;
        try {
            userId = JWTUtils.getUserId(request.getHeader("token"));

        }catch (Exception e){
            return (Result<?>) Result.NOTLOGIN("登录过期","/login");
        }
        chapter.setGmtCreate(new Date());
        chapter.setGmtModified(new Date());
        chapter.setUserId(userId);
        int i = chapterMapper.insert(chapter);
        if (i<1){
            return Result.error("-1","新增章节失败！");
        }
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Chapter chapter ,HttpServletRequest request) {
        Long userId ;
        try {
            userId = JWTUtils.getUserId(request.getHeader("token"));

        }catch (Exception e){
            return (Result<?>) Result.NOTLOGIN("登录过期","/login");
        }
        chapter.setGmtModified(new Date());

        Chapter c = chapterMapper.selectOne(Wrappers.<Chapter>lambdaQuery().eq(Chapter::getId, chapter.getId()).eq(Chapter::getUserId,userId));
        if (c != null) {
            int i = chapterMapper.updateById(chapter);
            if (i<1){
                return Result.error("-1","修改章节失败！");
            }
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id ,HttpServletRequest request) {
        Long userId ;
        try {
            userId = JWTUtils.getUserId(request.getHeader("token"));

        }catch (Exception e){
            return (Result<?>) Result.NOTLOGIN("登录过期","/login");
        }
        Chapter chapter = chapterMapper.selectOne(Wrappers.<Chapter>lambdaQuery().eq(Chapter::getId, id).eq(Chapter::getUserId,userId));
        if (chapter != null) {
            chapterMapper.deleteById(id);
            return Result.success();
        }
        return Result.error("403", "你没有权限");

    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,  HttpServletRequest request) {
        Long userId ;
        try {
            userId = JWTUtils.getUserId(request.getHeader("token"));
        }catch (Exception e){
            return (Result<?>) Result.NOTLOGIN("登录过期","/login");
        }
        Page<Chapter> pages = chapterMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<Chapter>lambdaQuery().like(Chapter::getName, search).eq(Chapter::getUserId,userId));
        return Result.success(pages);
    }
    @GetMapping("getBookAllChapter/{id}")
    public Result<?> findPage( @PathVariable Long id ) {
        List<Chapter> Chapters = chapterMapper.selectList(Wrappers.<Chapter>lambdaQuery().eq(Chapter::getId,id ));
        return Result.success(Chapters);
    }


    @GetMapping("getChapterByBookId/{id}")
    @NoAuth
    public Result<?> getChapterByBookId( @PathVariable Long id){
        List<Chapter> chapters = chapterMapper.selectList(Wrappers.<Chapter>lambdaQuery().eq(Chapter::getBookId, id));
        return Result.success(chapters);
    }

    @GetMapping("getChapterById/{id}")
    @NoAuth
    public Result<?> getChapterById( @PathVariable Long id){
        Chapter chapter = chapterMapper.selectById(id);
        return Result.success(chapter);
    }
}
